<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Open a ZIP file archive</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="ziparchive.locatename.html">ZipArchive::locateName</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ziparchive.renameindex.html">ZipArchive::renameIndex</a></div>
 <div class="up"><a href="class.ziparchive.html">ZipArchive</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="ziparchive.open" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">ZipArchive::open</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.2.0, PECL zip &gt;= 1.1.0)</p><p class="refpurpose"><span class="refname">ZipArchive::open</span> &mdash; <span class="dc-title">Open a ZIP file archive</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-ziparchive.open-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>ZipArchive::open</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code></span>
  ] )</div>

  <p class="para rdfs-comment">
   Opens a new zip archive for reading, writing or modifying.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-ziparchive.open-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">filename</code></em></dt>

     <dd>

      <p class="para">
       The file name of the ZIP archive to open.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">flags</code></em></dt>

     <dd>

      <p class="para">
       The mode to use to open the archive.
       <ul class="itemizedlist">
        <li class="listitem">
         <p class="para">
          <strong><code>ZipArchive::OVERWRITE</code></strong>
         </p>
        </li>
        <li class="listitem">
         <p class="para">
          <strong><code>ZipArchive::CREATE</code></strong>
         </p>
        </li>
        <li class="listitem">
         <p class="para">
          <strong><code>ZipArchive::EXCL</code></strong>
         </p>
        </li>
        <li class="listitem">
         <p class="para">
          <strong><code>ZipArchive::CHECKCONS</code></strong>
         </p>
        </li>
       </ul>
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-ziparchive.open-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">Error codes</code></em></dt>

      <dd>

      <p class="para">
       Returns <strong><code>TRUE</code></strong> on success or the error code.
       <ul class="itemizedlist">
        <li class="listitem">
         <p class="para">
          <strong><code>ZipArchive::ER_EXISTS</code></strong>
         </p>
         <p class="para">
          File already exists.
         </p>
        </li>
        <li class="listitem">
         <p class="para">
          <strong><code>ZipArchive::ER_INCONS</code></strong>
         </p>
         <p class="para">
          Zip archive inconsistent.
         </p>
        </li>
        <li class="listitem">
         <p class="para">
          <strong><code>ZipArchive::ER_INVAL</code></strong>
         </p>
         <p class="para">
          Invalid argument.
         </p>
        </li>
        <li class="listitem">
         <p class="para">
          <strong><code>ZipArchive::ER_MEMORY</code></strong>
         </p>
         <p class="para">
          Malloc failure.
         </p>
        </li>
        <li class="listitem">
         <p class="para">
          <strong><code>ZipArchive::ER_NOENT</code></strong>
         </p>
         <p class="para">
          No such file.
         </p>
        </li>
        <li class="listitem">
         <p class="para">
          <strong><code>ZipArchive::ER_NOZIP</code></strong>
         </p>
         <p class="para">
          Not a zip archive.
         </p>
        </li>
        <li class="listitem">
         <p class="para">
          <strong><code>ZipArchive::ER_OPEN</code></strong>
         </p>
         <p class="para">
          Can&#039;t open file.
         </p>
        </li>
        <li class="listitem">
         <p class="para">
          <strong><code>ZipArchive::ER_READ</code></strong>
         </p>
         <p class="para">
          Read error.
         </p>
        </li>
        <li class="listitem">
         <p class="para">
          <strong><code>ZipArchive::ER_SEEK</code></strong>
         </p>
         <p class="para">
          Seek error.
         </p>
        </li>
       </ul>
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-ziparchive.open-examples">
  <h3 class="title">范例</h3>
    <div class="example" id="example-775">
     <p><strong>Example #1 Open and extract</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$zip&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">ZipArchive</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$zip</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">open</span><span style="color: #007700">(</span><span style="color: #DD0000">'test.zip'</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'ok'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$zip</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">extractTo</span><span style="color: #007700">(</span><span style="color: #DD0000">'test'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$zip</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">close</span><span style="color: #007700">();<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'failed,&nbsp;code:'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$res</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
    <div class="example" id="example-776">
     <p><strong>Example #2 Create an archive</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$zip&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">ZipArchive</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$zip</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">open</span><span style="color: #007700">(</span><span style="color: #DD0000">'test.zip'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">ZipArchive</span><span style="color: #007700">::</span><span style="color: #0000BB">CREATE</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$zip</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addFromString</span><span style="color: #007700">(</span><span style="color: #DD0000">'test.txt'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'file&nbsp;content&nbsp;goes&nbsp;here'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$zip</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addFile</span><span style="color: #007700">(</span><span style="color: #DD0000">'data.txt'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'entryname.txt'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$zip</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">close</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'ok'</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'failed'</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="ziparchive.locatename.html">ZipArchive::locateName</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ziparchive.renameindex.html">ZipArchive::renameIndex</a></div>
 <div class="up"><a href="class.ziparchive.html">ZipArchive</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
